﻿using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;
using LsTool.ApiModels;

namespace Zapi.Api.Filters
{
    /// <summary>
    /// 获取头
    /// </summary>
    public class HeadersBinder : IModelBinder
    {

        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
            var model = valueProviderResult.ConvertTo(bindingContext.ModelType);

            var fb = bindingContext.Model as ITokenIn;
            if (fb != null)
                fb.Token = HttpContext.Current.Request.Headers["token"];

            bindingContext.Model = model;
            return true;
        }
    }
}